﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BL.BIZControls
{
    public partial class PageContainer : ControlContainerBase
    {
        public event ControlLoaded BIZControlLoaded;

        protected void Page_Load(object sender, EventArgs e)
        {
        
        }

    


        protected override void OnLoad(EventArgs e)
        {
            //AddControl();
            base.OnLoad(e);
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            AddControl();
        }



        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ContainedControlContainer = phControl;
            ButtonsControl = btnGUI;
            btnGUI.ButtonsClick += new ButtonsHandler(btnGUI_ButtonsClick);

            litTitle.Text = Title;

            
        }

        
        protected override void Render(HtmlTextWriter writer)
        {
            litTitle.Text = Title;
            base.Render(writer);
        }

        void btnGUI_ButtonsClick(object sender, ActionType actionType)
        {

            CallButtonEvent(this, actionType);

        }

        /// <summary>
        /// Remove added control
        /// </summary>
        public void Clear()
        {
            phControl.Controls.Clear();
            ControlPath = null;
            ContainedControl = null;
        }

        public void AddControl(BIZControl typeOfControl)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();
        }

        private void AddControl()
        {
            ContainedControl = null;
            if (ControlPath != null)
            {

                if (phControl.Controls.Count == 0)
                {

                    Control uc = (Control)this.LoadControl(ControlPath);
                    ContainedControl = uc;

                    if (ContainedControl is IContainedControl)
                    {
                        ((IContainedControl)ContainedControl).ContainerControl = this;
                    }
                    phControl.Controls.Add(ContainedControl);
                    btnGUI.SetValGroupPrefix(ValGroupPrefix);


                }
                else
                {
                    ContainedControl = phControl.Controls[0];
                }




                if (ContainedControl != null)
                {

                    if (BIZControlLoaded != null)
                    {
                        BIZControlLoaded(this, new EventArgs());
                    }

                }

            }

        }


        public void AddDefaultEditNew(BIZControl typeOfControl)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();

            ((IBIZControlDefaultMethods)ContainedControl).DefaultEditNew();

        }
        public void AddDefaultEditNew(BIZControl typeOfControl, Guid Id, string editByType)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();

            ((IBIZControlDefaultMethods)ContainedControl).DefaultEditNew(Id, editByType);

        }
        public void AddDefaultEditLoad(BIZControl typeOfControl, Guid Id)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();

            ((IBIZControlDefaultMethods)ContainedControl).DefaultEditLoad(Id);
        }
        public void AddDefaultListLoad(BIZControl typeOfControl)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();

            ((IBIZControlDefaultMethods)ContainedControl).DefaultListLoad();

        }
        public void AddDefaultListLoad(BIZControl typeOfControl, Guid Id, string loadByType)
        {

            ControlPath = GetControlPath(typeOfControl);
            AddControl();

            ((IBIZControlDefaultMethods)ContainedControl).DefaultListLoad(Id, loadByType);

        }
    }
}